package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.Color;
import com.isorf.service.ColorService;
import com.isorf.util.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "颜色管理")
@RestController
@RequestMapping("/color")
public class ColorController {
    @Autowired
    private ColorService colorService;

    /**
     * 新增颜色
     */
    @PostMapping("/add")
    public ResponseResult<Boolean> addColor(@RequestBody Color color) {
        Boolean result = colorService.addColor(color);
        if (result) {
            return ResponseResult.success(ResponseResult.SUCCESS, "添加成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "添加失败");
        }
    }

    /**
     * 删除颜色
     */
    @PostMapping("/delete")
    public ResponseResult<Boolean> deleteColor(@RequestParam int colorId) {
        Boolean result = colorService.deleteColor(colorId);
        if (result) {
            return ResponseResult.success(ResponseResult.SUCCESS, "删除成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "删除失败");
        }
    }

    /**
     * 更新颜色
     */
    @PostMapping("/update")
    public ResponseResult<Boolean> updateColor(@RequestBody Color color) {
        Boolean result = colorService.updateColor(color);
        if (result) {
            return ResponseResult.success(ResponseResult.SUCCESS, "更新成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "更新失败");
        }
    }

    /**
     * 根据ID查询颜色
     */
    @GetMapping("/getById")
    public ResponseResult<Color> getColorById(@RequestParam int colorId) {
        Color result = colorService.getColorById(colorId);
        if (result != null) {
            return ResponseResult.success(ResponseResult.SUCCESS, "查询成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "查询失败");
        }
    }

    /**
     * 分页查询颜色
     */
    @GetMapping("/pageList")
    public ResponseResult<PageInfo<Color>> pageList(@RequestParam(defaultValue = "1") int pageNum, 
                                                    @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<Color> result = colorService.pageList(pageNum, pageSize);
        if (result != null && result.getList().size() > 0) {
            return ResponseResult.success(ResponseResult.SUCCESS, "查询成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "查询失败");
        }
    }

    /**
     * 查询全部颜色
     */
    @GetMapping("/getAll")
    public ResponseResult<List<Color>> getAllColors() {
        List<Color> result = colorService.getAllColors();
        if (result != null && result.size() > 0) {
            return ResponseResult.success(ResponseResult.SUCCESS, "查询成功", result);
        } else {
            return ResponseResult.failure(ResponseResult.FAILED, "没有颜色信息");
        }
    }
} 